<?php
namespace app\Backend\controller;;
use think\Db;
use clt\Leftnav;
use app\Backend\model\Admin;
use app\Backend\model\AuthGroup;
use app\common\model\AuthRule;
use think\Validate;
class Staff extends Common
{
    public function _initialize(){
        parent::_initialize();
    }
    public function index()
    {
        return $this->fetch();
    }

    //员工列表
    public function staffList(){
        $val=input('val');
        $url['val'] = $val;
        $this->assign('testval',$val);
        $map='';
        if($val){
            $map['a.username|a.email|a.tel']= array('like',"%".$val."%");
        }

        $list=Db::table(config('database.prefix').'admin')->alias('a')
            ->join(config('database.prefix').'auth_group ag','a.group_id = ag.group_id','left')
            ->field('a.*,ag.title')
            ->where($map)
            ->select();
        $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        $this->assign('lists',$result['data']);
        return view();
    }

    //添加员工
    public function staffAdd(){
        if(request()->isPost()){
            $data = input('post.');
            $check_user = Admin::get(['username'=>$data['username']]);

            if ($check_user) {
                return $result = ['code'=>0,'msg'=>'用户已存在，请重新输入用户名!'];
            }
            $data['pwd'] = input('post.pwd', '', 'md5');
            $data['add_time'] = time();
            $data['ip'] = request()->ip();
            //验证
            $msg = $this->validate($data,'app\Backend\validate\Admin');
            if($msg!='true'){
                return $result = ['code'=>0,'msg'=>$msg];
            }
            //单独验证密码
            $checkPwd = Validate::is(input('post.pwd'),'require');
            if (false === $checkPwd) {
                return $result = ['code'=>0,'msg'=>'密码不能为空！'];
            }
            if(!empty($data['email']) && !is_email($data['email'])){
                return $result = ['code'=>0,'msg'=>'输入邮箱格式不对'];
            }
            //添加成功
            if (Admin::create($data)) {
                return ['code'=>1,'msg'=>'管理员添加成功!','url'=>'/reload'];
            } else {
                return ['code'=>0,'msg'=>'管理员添加失败!'];
            }
        }else{
            $auth_group=db('auth_group')->select();
            $this->assign('authGroup',$auth_group);
            $this->assign('title',lang('add').lang('admin'));
            $this->assign('info','null');
            $this->assign('selected', 'null');
            return view('staffForm');
        }
    }

    //删除管理员
    public function staffDel(){
        $admin_id=input('admin_id');
        $map['admin_id'] = $admin_id;
        $map['is_sys'] = 0; //0=>不是系统管理员，可删除!
        if (db('admin')->where($map)->delete()){
            return $result = ['code'=>1,'msg'=>'删除成功!','url'=>'/reload'];
        }else{
            return $result = ['code'=>0,'msg'=>'用户信息删除失败!','url'=>''];
        }
    }

    //修改管理员状态
    public function adminState(){
        $id=input('post.id');
        if (empty($id)){
            $result['status'] = 0;
            $result['info'] = '用户ID不存在!';
            $result['url'] = url('adminList');
            exit;
        }
        $status=db('admin')->where('admin_id='.$id)->value('is_open');//判断当前状态情况
        if($status==1){
            $data['is_open'] = 0;
            db('admin')->where('admin_id='.$id)->update($data);
            $result['status'] = 1;
            $result['open'] = 0;
        }else{
            $data['is_open'] = 1;
            db('admin')->where('admin_id='.$id)->update($data);
            $result['status'] = 1;
            $result['open'] = 1;
        }
        return $result;
    }

    //更新管理员信息
    public function staffEdit(){
        if(request()->isPost()){
            $data   = input('post.');
            $pwd    = input('post.pwd');

            $map['admin_id']    = array('neq',input('post.admin_id'));
            $where['admin_id']  = input('post.admin_id');
            if(input('post.username')){
                $map['username'] = input('post.username');
                $check_user = Admin::get($map);
                if ($check_user) {
                    return $result = ['code'=>0,'msg'=>'用户已存在，请重新输入用户名!'];
                }
            }
            if ($pwd){
                $data['pwd']=input('post.pwd','','md5');
            }else{
                unset($data['pwd']);
            }
            $msg = $this->validate($data,'Admin');
            if($msg!='true'){
                return $result = ['code'=>0,'msg'=>$msg];
            }
            if(!empty($data['email']) && !is_email($data['email'])){
                return $result = ['code'=>0,'msg'=>'输入邮箱格式不对'];
            }
            Admin::update($data);
            return $result = ['code'=>1,'msg'=>'管理员修改成功!','url'=>'/reload'];
        }else{
            $auth_group=db('auth_group')->select();
            $info = Admin::get(['admin_id'=>input('admin_id')]);
            $this->assign('row', $info);
            $this->assign('authGroup',$auth_group);
            $this->assign('title',lang('edit').lang('admin'));
            return view('staffForm');
        }
    }

    /*-----------------------用户组管理----------------------*/


    /*-----------------------权限管理------------------------*/

}